﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Server.MessageModule.Message
{
    public class BaseNetworkMessage : INetworkMessage
    {
        protected string _type = "";

        public BaseNetworkMessage(string type)
        {
            _type = type;
        }

        public string getType()
        {
            return _type;
        }

        public virtual string Serialize()
        {
            return null;
        }

        public virtual void Deserialize(string data)
        {
            try
            {
                if (validData(data))
                {
                    Dictionary<string, object> dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(data);
                    _type = (string)dic[ShareMacro.TAG_MSGTYPE];
                }
                
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("BaseNetWork: Deserialize error-" + e.Message);
            }
        }

        private Boolean validData(string data)
        {
            return data[0] == '{' && data[data.Length - 1] == '}';
        }
    }
}
